package wasnake.gui;

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;

import wasnake.engine.Engine;

/**
 * @author Brett Gavin
 */
public class TitleFrame extends JFrame {
	private final static long serialVersionUID = 200L;
	private JPanel foregroundPanel;
	private JButton easyButton, medButton, hardButton, instructionsButton;
	private GridPanel panel;
	private Engine engine;
	private JFrame snakeFrame;
	/**
	 * Wraps a Swing JComponent in a background image. Simply invokes the
	 * overloded variant with Top/Leading alignment for background image.
	 * 
	 * @param component
	 *            - to wrap in the a background image
	 * @param backgroundIcon
	 *            - the background image (Icon)
	 * @return the wrapping JPanel
	 */
	public static JPanel wrapInBackgroundImage(JComponent component,
			Icon backgroundIcon) {
		return wrapInBackgroundImage(component, backgroundIcon, JLabel.TOP,
				JLabel.LEADING);
	}

	/**
	 * Wraps a Swing JComponent in a background image. The vertical and
	 * horizontal alignment of background image can be specified using the
	 * alignment contants from JLabel.
	 * 
	 * @param component
	 *            - to wrap in the a background image
	 * @param backgroundIcon
	 *            - the background image (Icon)
	 * @param verticalAlignment
	 *            - vertical alignment. See contants in JLabel.
	 * @param horizontalAlignment
	 *            - horizontal alignment. See contants in JLabel.
	 * @return the wrapping JPanel
	 */
	public static JPanel wrapInBackgroundImage(JComponent component,
			Icon backgroundIcon, int verticalAlignment, int horizontalAlignment) {

		// make the passed in swing component transparent
		component.setOpaque(false);

		// create wrapper JPanel
		JPanel backgroundPanel = new JPanel(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		backgroundPanel.add(component, gbc);

		// create a label to paint the background image
		JLabel backgroundImage = new JLabel(backgroundIcon);

		// set minimum and preferred sizes so that the size of the image
		// does not affect the layout size
		backgroundImage.setPreferredSize(new Dimension(1, 1));
		backgroundImage.setMinimumSize(new Dimension(1, 1));

		// align the image as specified.
		backgroundImage.setVerticalAlignment(verticalAlignment);
		backgroundImage.setHorizontalAlignment(horizontalAlignment);

		// add the background label
		backgroundPanel.add(backgroundImage, gbc);

		// return the wrapper
		return backgroundPanel;
	}

	public void setGui() {
		foregroundPanel = new JPanel(new BorderLayout(10, 10));
		foregroundPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		foregroundPanel.setLayout(new GridBagLayout());
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		easyButton = new JButton("Easy");
		c.weightx = 1;
		c.gridwidth = 1;
		c.ipady = 0;
		c.gridx = 0;
		c.gridy = 0;
		c.insets = new Insets(10, 10, 10, 10);
		foregroundPanel.add(easyButton, c);
		
		medButton = new JButton("Medium");
		c.gridx = 1;
		c.gridy = 0;
		c.insets = new Insets(10, 10, 10, 10);
		foregroundPanel.add(medButton, c);
		
		hardButton = new JButton("Hard");
		c.gridx = 2;
		c.gridy = 0;
		c.insets = new Insets(10, 10, 10, 10);
		foregroundPanel.add(hardButton, c);
		
		instructionsButton = new JButton("Instructions");
		c.gridx = 1;
		c.gridy = 1;
		c.insets = new Insets(10, 10, 10, 10);
		foregroundPanel.add(instructionsButton, c);
		
		foregroundPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10,
				10));
		foregroundPanel.setOpaque(false);

		setContentPane(wrapInBackgroundImage(foregroundPanel, new ImageIcon(
				TitleFrame.class.getResource("snake.jpg"))));
	}
	

	public TitleFrame() {
		super("Snake");
		setGui();
		
		easyButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				panel = new  GridPanel(new BorderLayout(10, 10));
				//TODO decide what size the grid should be and figure out difficulty
				//TODO need to figure out when to initialize grid
				engine = new Engine(50, 1, panel);
				engine.run();
				snakeFrame = new JFrame("Snake");
				panel.requestFocus();
				panel.addKeyListener(new KeyAdapter() {
		        	//Method for the key pressed
		            public void keyPressed(KeyEvent evt) {
		                formKeyPressed(evt);
		            }
		        });
				
				snakeFrame.add(panel);
				snakeFrame.setSize(500, 540);
				snakeFrame.setVisible(true);
				panel.requestFocus();
			}
		});
		
		medButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				panel = new  GridPanel(new BorderLayout(10, 10));
				//TODO decide what size the grid should be and figure out difficulty
				//TODO need to figure out when to initialize grid
				engine = new Engine(50, 2, panel);
				engine.run();
				snakeFrame = new JFrame("Snake");
				panel.requestFocus();
				panel.addKeyListener(new KeyAdapter() {
		        	//Method for the key pressed
		            public void keyPressed(KeyEvent evt) {
		                formKeyPressed(evt);
		            }
		        });
				
				snakeFrame.add(panel);
				snakeFrame.setSize(500, 540);
				snakeFrame.setVisible(true);
				panel.requestFocus();
			}
		});
		
		hardButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				panel = new  GridPanel(new BorderLayout(10, 10));
				//TODO decide what size the grid should be and figure out difficulty
				//TODO need to figure out when to initialize grid
				engine = new Engine(50, 3, panel);
				engine.run();
				snakeFrame = new JFrame("Snake");
				panel.requestFocus();
				panel.addKeyListener(new KeyAdapter() {
		        	//Method for the key pressed
		            public void keyPressed(KeyEvent evt) {
		                formKeyPressed(evt);
		            }
		        });
				
				snakeFrame.add(panel);
				snakeFrame.setSize(500, 540);
				snakeFrame.setVisible(true);
				panel.requestFocus();
			}
		});
		
		instructionsButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				JOptionPane.showMessageDialog(null, "To play our game, you must first pick a difficulty level easy, medium, or hard. \nEasy is really slow, and you will get 1 point per food you eat.\n Medium is a little faster, and you will get 2 points per food you eat. \nHard is really fast, and you will recieve 3 points per food you eat. \nThe objective of the game is to eat the food, which is orange. \nWhile eating food you must avoid the obstacles which include:\n      moose- moves around the screen as an obstacle and is black\n      yourself\n      the sides \nIf you hit any of the obstacles the game will be over. \nEach time you eat a food the snake will get one square longer");
			}
		});

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(405,400);
		setVisible(true);
	}
	
	private void formKeyPressed(KeyEvent evt)
    {
        engine.keyPressed(evt);
    }

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new TitleFrame();
			}
		});
	}

}

